package Survey;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class CreatorModule extends JPanel {

	JPanel top, main;
	int index;
	SurveyParameters data;
	Dimension totalSize;
	JButton visible;
	Vector<Boolean> showing;
	JTextField question, number;
	JLabel imageLoc;
	
	public CreatorModule(SurveyParameters data, Vector<Boolean> showing){
		this.data = data;
		this.showing = showing;
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createLineBorder(Color.black));
		setBackground(Color.white);
		setOpaque(true);
		
		top = new JPanel();
		top.setBackground(Color.white);
		top.setOpaque(true);
		top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
		
		visible = new JButton("Hide");
		visible.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
 				CreatorModule cmodule = ((CreatorModule)((JButton)e.getSource()).getParent().getParent());
				
				if(cmodule.showing.get(cmodule.data.index - 1).booleanValue()){
					cmodule.main.setVisible(false);
					((JButton)e.getSource()).setText("Show");
					cmodule.setPreferredSize(SurveyCreatorGui.TOPSIZE);
					cmodule.showing.set(cmodule.data.index - 1, Boolean.valueOf(false));
				}else{
					cmodule.main.setVisible(true);
					((JButton)e.getSource()).setText("Hide");
					cmodule.setPreferredSize(totalSize);
					cmodule.showing.set(cmodule.data.index - 1, Boolean.valueOf(true));
				}
				if(cmodule.getParent() != null){
					((SurveyCreatorGui)cmodule.getTopLevelAncestor()).updateSize();
				}
			}
		});
		
		number = new JTextField("" + data.index, 2);
		number.setMaximumSize(number.getPreferredSize());
		number.addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {return;}
			public void focusLost(FocusEvent e) {
				try{
					int i = Integer.parseInt(((JTextField)e.getSource()).getText().trim());
					if(i < 1){
						throw(new NumberFormatException());
					}
					((CreatorModule)((Component) e.getSource()).getParent().getParent()).data.index = i;
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
						"" + ((CModuleText)((Component) e.getSource()).getParent().getParent()).data.index);
							
				}
			}
		});
		
		JCheckBox multipart = new JCheckBox("Multi-Part");
		if(data.multipart){
			multipart.setSelected(true);
		}
		multipart.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				((CreatorModule)((Component) e.getSource()).getParent().getParent()).data.multipart = ((JCheckBox)e.getSource()).isSelected();
			}
		});
		
		
		question = new JTextField(data.question);
		question.addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {return;}
			public void focusLost(FocusEvent e) {
				((CreatorModule)((Component) e.getSource()).getParent().getParent()).data.question = ((JTextField)e.getSource()).getText();		
			
				if(!((CreatorModule)((Component) e.getSource()).getParent().getParent()).data.question.equals("")){
					((JTextField)e.getSource()).setBackground(Color.white);
				}
			
			}
		});
		question.setMaximumSize(new Dimension(SurveyCreatorGui.WIDTH - 20,22));
		
		JButton remove = new JButton("Delete");
		remove.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				CreatorModule cmod = ((CreatorModule)((JButton)e.getSource()).getParent().getParent());
				SurveyCreatorGui scg = ((SurveyCreatorGui)cmod.getTopLevelAncestor());
				cmod.data.index = scg.index;
				scg.updateList();
				scg.container.remove(scg.index - 1);
				scg.surveys.remove(scg.index - 1);
				scg.visible.remove(scg.index - 1);
				scg.location.remove(scg.index);
				scg.index--;
				scg.updateSize();
			}
		});
		
	
		main = new JPanel();
		main.setBackground(Color.white);
		main.setOpaque(true);
		main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
		
		JPanel main1 = new JPanel();
		main1.setBackground(Color.white);
		main1.setOpaque(true);
		main1.setLayout(new BoxLayout(main1, BoxLayout.X_AXIS));
		imageLoc = new JLabel((data.picture == null || data.picture.getIconHeight() == -1) ? "" : data.picture.getDescription());
		imageLoc.setMinimumSize(new Dimension(500, 1));
		imageLoc.setPreferredSize(new Dimension(500, 20));
		JButton fileChooser = new JButton("Browse");
		fileChooser.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();		
				CreatorModule cmod = (CreatorModule) ((JButton)e.getSource()).getParent().getParent().getParent();
				int retVal = jfc.showOpenDialog(((JButton)e.getSource()).getTopLevelAncestor());
				if (retVal == JFileChooser.APPROVE_OPTION) {
		            File file = jfc.getSelectedFile();
		            ImageIcon ii = new ImageIcon(file.getAbsolutePath());
		            
		            if(ii.getIconHeight() == -1){
		            	return;
		            }
		            cmod.data.picture = ii;
		            cmod.imageLoc.setText(file.getAbsolutePath());
		        }
			}
		});
		
		main1.add(fileChooser);
		main1.add(new JLabel("Image: "));
		main1.add(imageLoc);
		main1.add(Box.createHorizontalGlue());
		
		top.add(visible);
		top.add(new JLabel("Question #"));
		top.add(number);
		top.add(new JLabel(" : "));
		top.add(question);
		top.add(multipart);
		top.add(remove);
		
		main.add(main1);
		
		add(top, BorderLayout.NORTH);
		add(main, BorderLayout.CENTER);
		
		
	}
	
	public abstract void highlight(int i);
	
}
